/* 	Estimating Option Values and Spillover Damages for Coastal Protection: 
		Evidence from Oregon’s Planning Goal 18
	Journal of the Association of Environmental and Resource Economists 
		Steven J. Dundas  and David J. Lewis		2020 */ 
		
# delimit ;
set more off ;
capture log close ;
clear all ;
cd "C:\Users\dundas\Desktop\JAERE_DataCode\replication" ;


/* ********************************* TABLES ******************************* */

/* ******** Table 1: Summary Statistics by Protection Eligibility Status**** */
use ..\replication\data\oceanfront_sales.dta, clear;
/* DATASET INCLUDES: 1,519 observations: 
		1,011 eligible (treated)
		418 not eligible (control) */
		
/* Table 1 Column 1 */
sum pr mktimprovementvalue lrr_lt elev_min setback age bed bath sqfeet
		lot_sqft gar ac d_lighthou bluff dune dist_mhw flood_100 protect1 d_sps
		CRS NFIPsub year county latit longi if g18_bin==1 ; 
/* Table 1 Column 2 */
sum pr mktimprovementvalue lrr_lt elev_min setback age bed bath sqfeet
		lot_sqft gar ac d_lighthou bluff dune dist_mhw flood_100 protect1 d_sps
		CRS NFIPsub year county latit longi if g18_bin==0 ;  
clear all; 

/* Data Trimming: 1-nearest neighbor matching using propensity score ******* */ 
use ..\replication\data\oceanfront_sales.dta, clear ; 
logit g18_bin mktimprovementvalue bed bath lot_sqft dist_mhw bluff setback 
	age pr county year latit longi ; predict ps ; drop id; gen id = _n ;  
tempfile ps; save `ps', replace ; tempfile ps1; 
nnmatch g18_bin g18_bin ps,	keep(`ps1') replace ; 
	clear all; 
use `ps1', clear ;  
 keep if g18_bin==0;   sort id ; 
		quietly by id:  gen dup = cond(_N==1,0,_n); tab dup ; 
		drop if dup>1; drop dup; 
tempfile control;  save `control', replace; clear all ; 
/* all non-eligible transactions*/
use `ps1', clear ;  
 keep if g18_bin==0;  
 sort id ; 	drop id; rename index id ;
	sort id ; 	quietly by id:  gen dup = cond(_N==1,0,_n); tab dup ; 
/* keep best matched eligible parcels */
tempfile treated; save `treated', replace ;  
	append using `control';		sort id ;
	tempfile matches ; save `matches', replace ;	clear all ;		
use `ps', clear ;
	merge 1:m id using `matches', keep(match) ;  
/* This is now the trimmed dataset ---- GIS variables to quantify spillover 
impacts added to main estimation file outside of Stata to generate main 
estimation file */ 
clear all ;
/* DATASET INCLUDES: 866 observations: 
		448 eligible (treated)
		418 not eligible (control)
		Data trimmed using k-nearest neighbor matching algorithm (k=1) 
		with replacement that keeps all controls and matches to treated 
		observations based on estimated propensity score */
use ..\replication\data\main_estimation.dta, clear;		
/* Table 1 Column 3 */
sum pr mktimprovementvalue lrr_lt elev_min setback age bed bath sqfeet
		lot_sqft gar ac d_lighthou bluff dune dist_mhw flood_100 protect1 d_sps
		CRS NFIPsub year county latit longi if g18_bin==1 ; 
clear all ;
 
/* ******** Table 2: Post-Match Regression Results for Baseline Model ****** */
use ..\replication\data\main_estimation.dta, clear; 
/* Table 2 column (1) */
reg log_p15 g18_bin bed bath sqfeet lot_sqft age gar ac bed_sq bath_sq age_sq
	sqfeet_sq lotsqft_sq finbase ,cluster(city); 
/* Table 2 column (2)*/
reg log_p15 g18_bin g18_eroLr g18emin g18sub elev_min lrr_lt setback bed bath
	 sqfeet lot_sqft age gar ac dist_mhw bluff dune flood_100 d_lighthou 
	 protect1 d_sps CRS NFIPsub bed_sq bath_sq sqfeet_sq lotsqft_sq age_sq 
	 dmhw_sq dsps_sq landslide finbase, cluster(city);  
/* Table 2 column (3)*/
reg log_p15 g18_bin g18_eroLr g18emin g18sub elev_min lrr_lt setback bed bath
	 sqfeet lot_sqft age gar ac dist_mhw bluff dune flood_100 d_lighthou 
	 protect1 d_sps CRS NFIPsub bed_sq bath_sq sqfeet_sq lotsqft_sq age_sq 
	 dmhw_sq dsps_sq landslide finbase i.quarter i.cy, cluster(city);  
clear all ; 

/* ******** Table 3: Post-Match Regression Results for Models Accounting for
					 Spillovers ******************************************** */
use ..\replication\data\main_estimation.dta, clear; 
/* Table 3 column (1)*/
reg log_p15 g18_bin g18_eroLr g18emin g18sub elev_min lrr_lt setback bed bath
	 sqfeet lot_sqft age gar ac dist_mhw bluff dune flood_100 d_lighthou 
	 protect1 d_sps CRS NFIPsub bed_sq bath_sq sqfeet_sq lotsqft_sq age_sq 
	 dmhw_sq dsps_sq landslide finbase i.quarter i.cy, cluster(city);  
/* Table 3 column (2)*/
reg log_p15 g18_bin g18_eroLr g18emin g18sub elev_min lrr_lt setback bed bath
	 sqfeet lot_sqft age gar ac dist_mhw bluff dune flood_100 d_lighthou 
	 protect1 d_sps CRS NFIPsub bed_sq bath_sq sqfeet_sq lotsqft_sq age_sq 
	 dmhw_sq dsps_sq landslide finbase i.quarter i.cy if d_1!=1, cluster(city); 
/* Table 3 column (3)*/
reg log_p15 g18_bin g18_eroLr g18emin g18sub elev_min lrr_lt setback bed bath
	 sqfeet lot_sqft age gar ac dist_mhw bluff dune flood_100 d_lighthou 
	 protect1 d_sps CRS NFIPsub bed_sq bath_sq sqfeet_sq lotsqft_sq age_sq 
	 dmhw_sq dsps_sq landslide finbase i.quarter i.cy if d_2!=1, cluster(city);
/* Table 3 column (4)*/
reg log_p15 g18_bin g18_eroLr g18emin g18sub elev_min lrr_lt setback bed bath
	 sqfeet lot_sqft age gar ac dist_mhw bluff dune flood_100 d_lighthou 
	 protect1 d_sps CRS NFIPsub bed_sq bath_sq sqfeet_sq lotsqft_sq age_sq 
	 dmhw_sq dsps_sq landslide finbase i.quarter i.cy if d_3!=1, cluster(city);
clear all ; 
 
/* Table 4: Discrete Change Effect of Goal 18 Eligibility ****************** */
/* PANEL A */
use ..\replication\data\main_estimation.dta, clear; 
quietly reg log_p15 g18_bin g18_eroLr g18emin g18sub elev_min lrr_lt setback bed
		bath sqfeet lot_sqft age gar ac dist_mhw bluff dune flood_100 d_lighthou 
		protect1 d_sps CRS NFIPsub bed_sq bath_sq sqfeet_sq lotsqft_sq age_sq 
		dmhw_sq dsps_sq landslide finbase i.quarter i.cy, cluster(city);  
/* Table 4 Panel A All parcels N=866*/
	quietly sum lrr_lt if g18_bin==1 ; local meanero = r(mean);
	quietly sum elev_min if g18_bin==1 ; local meanele = r(mean);
	quietly sum setback if g18_bin==1 ; local meanset = r(mean);
	quietly sum NFIPsub if g18_bin==1  ; local sub = r(mean);
nlcom _b[g18_bin] + _b[g18emin]*`meanele' + _b[g18_eroLr]*`meanero' 
		+_b[g18sub]*`sub';
/* Table 4 Panel A Eroding parcels N=246*/
	quietly sum lrr_lt if g18_bin==1 & lrr_lt<0 ; local meaneroE = r(mean);
	quietly sum elev_min if g18_bin==1 & lrr_lt<0 ; local meaneleE = r(mean);
	quietly sum NFIPsub if g18_bin==1 & lrr_lt<0 ; local sub1 = r(mean);
nlcom _b[g18_bin] + _b[g18emin]*`meaneleE' + _b[g18_eroLr]*`meaneroE' 
		+_b[g18sub]*`sub' ;
/* Table 4 Panel A Low Elev Parcel N=534 */
	quietly sum lrr_lt if g18_bin==1 & elev_min<=30 ; 
		local meaneroEM = r(mean);
	quietly sum elev_min if g18_bin==1 & elev_min<=30 ; 
		local meaneleEM = r(mean);
	quietly sum NFIPsub if g18_bin==1 & elev_min<=30 ; 
		local sub2 = r(mean);
nlcom _b[g18_bin] + _b[g18emin]*`meaneleEM' + _b[g18_eroLr]*`meaneroEM'
		+_b[g18sub]*`sub' ; 
/* Table 4 Panel A Low Elev, Eroding Parcel N=155 */
	quietly sum lrr_lt if g18_bin==1 & elev_min<=30 & lrr_lt<=0 ;
		local erodelow = r(mean);
	quietly sum elev_min if g18_bin==1 & elev_min<=30 & lrr_lt<=0 ;
		local lowerode = r(mean);
	quietly sum NFIPsub if g18_bin==1 & elev_min<=30 & lrr_lt<=0 ;
		local sub3 = r(mean);
nlcom _b[g18_bin] + _b[g18emin]*`lowerode' + _b[g18_eroLr]*`erodelow' 
		+_b[g18sub]*`sub'; 
clear all; 
/* PANEL B */
use ..\replication\data\main_estimation.dta, clear; 
quietly reg log_p15 g18_bin g18_eroLr g18emin g18sub elev_min lrr_lt setback bed
		bath sqfeet lot_sqft age gar ac dist_mhw bluff dune flood_100 d_lighthou 
		protect1 d_sps CRS NFIPsub bed_sq bath_sq sqfeet_sq lotsqft_sq age_sq 
		dmhw_sq dsps_sq landslide finbase i.quarter i.cy if d_1!=1,
		cluster(city);  
/* Table 4 Panel B All parcels N=679*/
	quietly sum lrr_lt if g18_bin==1 ; local meanero = r(mean);
	quietly sum elev_min if g18_bin==1 ; local meanele = r(mean);
	quietly sum setback if g18_bin==1 ; local meanset = r(mean);
	quietly sum NFIPsub if g18_bin==1  ; local sub = r(mean);
nlcom _b[g18_bin] + _b[g18emin]*`meanele' + _b[g18_eroLr]*`meanero' 
	+_b[g18sub]*`sub';
/* Table 4 Panel B Eroding parcels N=198*/
	quietly sum lrr_lt if g18_bin==1 & lrr_lt<0 ; local meaneroE = r(mean);
	quietly sum elev_min if g18_bin==1 & lrr_lt<0 ; local meaneleE = r(mean);
	quietly sum NFIPsub if g18_bin==1 & lrr_lt<0 ; local sub1 = r(mean);
nlcom _b[g18_bin] + _b[g18emin]*`meaneleE' + _b[g18_eroLr]*`meaneroE' 
	+_b[g18sub]*`sub' ;
/* Table 4 Panel B Low Elev Parcel N=404 */
	quietly sum lrr_lt if g18_bin==1 & elev_min<=30 ;
		local meaneroEM = r(mean);
	quietly sum elev_min if g18_bin==1 & elev_min<=30 ;
		local meaneleEM = r(mean);
	quietly sum NFIPsub if g18_bin==1 & elev_min<=30 ; 
		local sub2 = r(mean);
nlcom _b[g18_bin] + _b[g18emin]*`meaneleEM' + _b[g18_eroLr]*`meaneroEM' 
		+_b[g18sub]*`sub' ; 
/* Table 4 Panel B Low Elev, Eroding Parcel N=119 */
	quietly sum lrr_lt if g18_bin==1 & elev_min<=30 & lrr_lt<=0 ; 
		local erodelow = r(mean);
	quietly sum elev_min if g18_bin==1 & elev_min<=30 & lrr_lt<=0 ; 
		local lowerode = r(mean);
	quietly sum NFIPsub if g18_bin==1 & elev_min<=30 & lrr_lt<=0 ; 	
		local sub3 = r(mean);
nlcom _b[g18_bin] + _b[g18emin]*`lowerode' + _b[g18_eroLr]*`erodelow' 
		+_b[g18sub]*`sub'; 
clear all; 
/* PANEL C */
use ..\replication\data\main_estimation.dta, clear; 
quietly reg log_p15 g18_bin g18_eroLr g18emin g18sub elev_min lrr_lt setback bed
		bath sqfeet lot_sqft age gar ac dist_mhw bluff dune flood_100 d_lighthou 
		protect1 d_sps CRS NFIPsub bed_sq bath_sq sqfeet_sq lotsqft_sq age_sq 
		dmhw_sq dsps_sq landslide finbase i.quarter i.cy if d_2!=1, 
		cluster(city);  
/* Table 4 Panel C All parcels N=647*/
	quietly sum lrr_lt if g18_bin==1 ; local meanero = r(mean);
	quietly sum elev_min if g18_bin==1 ; local meanele = r(mean);
	quietly sum setback if g18_bin==1 ; local meanset = r(mean);
	quietly sum NFIPsub if g18_bin==1  ; local sub = r(mean);
nlcom _b[g18_bin] + _b[g18emin]*`meanele' + _b[g18_eroLr]*`meanero' 
		+_b[g18sub]*`sub';
/* Table 4 Panel C Eroding parcels N=187*/
	quietly sum lrr_lt if g18_bin==1 & lrr_lt<0 ; local meaneroE = r(mean);
	quietly sum elev_min if g18_bin==1 & lrr_lt<0 ; local meaneleE = r(mean);
	quietly sum NFIPsub if g18_bin==1 & lrr_lt<0 ; local sub1 = r(mean);
nlcom _b[g18_bin] + _b[g18emin]*`meaneleE' + _b[g18_eroLr]*`meaneroE' 
		+_b[g18sub]*`sub' ;
/* Table 4 Panel C Low Elev Parcel N=386 */
	quietly sum lrr_lt if g18_bin==1 & elev_min<=30 ; 
		local meaneroEM = r(mean);
	quietly sum elev_min if g18_bin==1 & elev_min<=30 ;
		local meaneleEM = r(mean);
	quietly sum NFIPsub if g18_bin==1 & elev_min<=30 ; 
		local sub2 = r(mean);
nlcom _b[g18_bin] + _b[g18emin]*`meaneleEM' + _b[g18_eroLr]*`meaneroEM' 
	+_b[g18sub]*`sub' ; 
/* Table 4 Panel C Low Elev, Eroding Parcel N=113 */
	quietly sum lrr_lt if g18_bin==1 & elev_min<=30 & lrr_lt<=0 ; 
		local erodelow = r(mean);
	quietly sum elev_min if g18_bin==1 & elev_min<=30 & lrr_lt<=0 ; 
		local lowerode = r(mean);
	quietly sum NFIPsub if g18_bin==1 & elev_min<=30 & lrr_lt<=0 ; 
		local sub3 = r(mean);
nlcom _b[g18_bin] + _b[g18emin]*`lowerode' + _b[g18_eroLr]*`erodelow' 
		+_b[g18sub]*`sub'; 
clear all ; 
/*PANEL D */
use ..\replication\data\main_estimation.dta, clear; 
quietly reg log_p15 g18_bin g18_eroLr g18emin g18sub elev_min lrr_lt setback bed
		bath sqfeet lot_sqft age gar ac dist_mhw bluff dune flood_100 d_lighthou 
		protect1 d_sps CRS NFIPsub bed_sq bath_sq sqfeet_sq lotsqft_sq age_sq 
		dmhw_sq dsps_sq landslide finbase i.quarter i.cy if d_3!=1, 
		cluster(city);  
/* Table 4 Panel D All parcels N=614*/
	quietly sum lrr_lt if g18_bin==1 ; local meanero = r(mean);
	quietly sum elev_min if g18_bin==1 ; local meanele = r(mean);
	quietly sum setback if g18_bin==1 ; local meanset = r(mean);
	quietly sum NFIPsub if g18_bin==1  ; local sub = r(mean);
nlcom _b[g18_bin] + _b[g18emin]*`meanele' + _b[g18_eroLr]*`meanero' 
		+_b[g18sub]*`sub';
/* Table 4 Panel D Eroding parcels N=178*/
	quietly sum lrr_lt if g18_bin==1 & lrr_lt<0 ; local meaneroE = r(mean);
	quietly sum elev_min if g18_bin==1 & lrr_lt<0 ; local meaneleE = r(mean);
	quietly sum NFIPsub if g18_bin==1 & lrr_lt<0 ; local sub1 = r(mean);
nlcom _b[g18_bin] + _b[g18emin]*`meaneleE' + _b[g18_eroLr]*`meaneroE' 
		+_b[g18sub]*`sub' ;
/* Table 4 Panel D Low Elev Parcel N=374 */
  quietly sum lrr_lt if g18_bin==1 & elev_min<=30; local meaneroEM = r(mean);
  quietly sum elev_min if g18_bin==1 & elev_min<=30; local meaneleEM = r(mean);
  quietly sum NFIPsub if g18_bin==1 & elev_min<=30; local sub2 = r(mean);
nlcom _b[g18_bin] + _b[g18emin]*`meaneleEM' + _b[g18_eroLr]*`meaneroEM' 
		+_b[g18sub]*`sub' ; 
/* Table 4 Panel D Low Elev, Eroding Parcel N=112 */
  quietly sum lrr_lt if g18_bin==1 & elev_min<=30 & lrr_lt<=0; 
	local erodelow = r(mean);
  quietly sum elev_min if g18_bin==1 & elev_min<=30 & lrr_lt<=0; 
	local lowerode = r(mean);
  quietly sum NFIPsub if g18_bin==1 & elev_min<=30 & lrr_lt<=0; 
	local sub3 = r(mean);
nlcom _b[g18_bin] + _b[g18emin]*`lowerode' + _b[g18_eroLr]*`erodelow' 
		+_b[g18sub]*`sub'; 

/* Table 5: Estimation of Spillover Effect ********************************* */
use ..\replication\data\main_estimation.dta, clear ;
/* Column 1 - results displayed are coefficients on sp_`i' variable from 
				separate regressions */
forv i = 1/5 { ;
reg log_p15 sp_`i' g18_bin g18_eroLr g18emin g18sub elev_min lrr_lt setback bed
	 bath sqfeet lot_sqft age gar ac dist_mhw bluff dune flood_100 d_lighthou 
	 protect1 d_sps CRS NFIPsub bed_sq bath_sq sqfeet_sq lotsqft_sq age_sq 
	 dmhw_sq dsps_sq landslide finbase i.quarter i.cy, cluster(city);  	} ; 
/* Column 2* - results displayed are coefficients on sp_`i' variable from 
				separate regressions */
forv i = 1/5 { ;
reg log_p15 sp_`i' lrr_lt elev_min setback bed bath sqfeet lot_sqft age gar ac
	dist_mhw bluff dune flood_100 d_lighthou bed_sq bath_sq sqfeet_sq lotsqft_sq
	age_sq dmhw_sq CRS NFIPsub i.quarter i.cy if g18_bin==0, cluster(county);  
	} ;  
/*Column 3*/
forv i = 1/5 { ;
reg log_p15 sp_`i' sp_er_`i' sp_el_`i' sp_sub_`i' elev_min lrr_lt setback bed 
	 bath sqfeet lot_sqft age gar ac dist_mhw bluff dune flood_100 d_lighthou 
	 protect1 d_sps CRS NFIPsub bed_sq bath_sq sqfeet_sq lotsqft_sq age_sq 
	 dmhw_sq dsps_sq landslide finbase i.quarter i.cy if g18_bin==0,
	 cluster(county);  
/* Discrete Change Effect, All parcels  */ 
	quietly sum lrr_lt if g18_bin==0 ; local meanero = r(mean);
	quietly sum elev_min if sp_`i'==1 ; local meanele = r(mean);
	quietly sum setback if sp_`i'==1 ; local meanset = r(mean);
	quietly sum NFIPsub if sp_`i'==1 ; local meansub = r(mean);
nlcom _b[sp_`i'] + _b[sp_el_`i']*`meanele' + _b[sp_er_`i']*`meanero' 
	+_b[sp_sub_`i']*`meansub' ; 
};  
clear all; 
	
/* ********************************* FIGURES ******************************* */

/* Figure 2: Propensity Score Histograms *********************************** */
use ..\replication\data\oceanfront_sales.dta, clear;
logit g18_bin mktimprovement mkt_sq	lot_sqft dist_mhw bluff setback price bed 
	bath age county year latit longi ;     predict ps ; 
		density2 ps, group(g18_bin) ;  clear all ;  
use ..\replication\data_FULL\main_estimation.dta, clear;
	density2 ps, group(g18_bin) ; clear all ;
	
/* Figure 5: Effect of Goal 18 Eligibility for Vunerable Parcels */
/* Panel A:  */
insheet using ..\replication\data\Fig5_panelA.csv , ;  
	twoway rarea low high scr, color("214 214 214") || scatter estimate scr, 
	color(black) c(1) lp(longdash) ytitle("Goal 18 Eligibility Effects") 
	 xtitle("Shoreline Change Rate (m/yr)") xlabel(-1.8(0.3)1.2, nogrid) 
	 ylabel(-0.2(0.1)0.7, nogrid ) || line origin scr, color(black) legend(off) 
	 plotregion(fcolor(white) lcolor(white)) 
	 graphregion(fcolor(white)lcolor(white)) subtitle("Panel A", pos(11)) ;
	clear all ; 
insheet using ..\replication\data\Fig5_panelB.csv  , ;  
	twoway rarea low high minelv, color("214 214 214") || scatter estimate 
		minelv, color(black) c(1) lp(longdash) 
		ytitle("Goal 18 Eligibility Effects") 
		xtitle("Minimum Parcel Elevation") xlabel(-0(5)50, nogrid) 
		ylabel(-0.2(0.1)0.7, nogrid) || line origin minelv, 
		color(black) legend(off) plotregion(fcolor(white) lcolor(white)) 
		graphregion(fcolor(white)lcolor(white)) subtitle("Panel B", pos(11)) ;
clear all ; 

/* ************************** THE END ************************************* */
	 
/* 	Estimating Option Values and Spillover Damages for Coastal Protection: 
		Evidence from Oregon’s Planning Goal 18
	Journal of the Association of Environmental and Resource Economists 
		Steven J. Dundas  and David J. Lewis		2020 */ 	 
	 
	 
	 
	 
	 
	 
